package com.springbootbase.springbootproject.service.impl;

import com.springbootbase.springbootproject.mapper.RecommendationcontactMapper;
import com.springbootbase.springbootproject.mapper.UserMapper;
import com.springbootbase.springbootproject.pojo.ListResponse;
import com.springbootbase.springbootproject.pojo.RecommendList;
import com.springbootbase.springbootproject.pojo.Recommendationcontact;
import com.springbootbase.springbootproject.service.RecommendationContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class RecommendationContactImpl implements RecommendationContactService {

    @Autowired
    private RecommendationcontactMapper recommendationcontactMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void sendInvite(Recommendationcontact recommendationcontact) {
        recommendationcontactMapper.sendInvite(recommendationcontact);
        recommendationcontactMapper.changeContactStatus(recommendationcontact.getStudentId(),recommendationcontact.getTeacherId());
    }

    @Override
    public ArrayList<Recommendationcontact> getInviteList(Long id) {
       ArrayList<Recommendationcontact> recommendationcontactArrayList = recommendationcontactMapper.getInviteList(id);
       for (Recommendationcontact recommendationcontact : recommendationcontactArrayList){
           String name1 = userMapper.getNameById(recommendationcontact.getStudentId());
           recommendationcontact.setStudentName(name1);
           name1 = userMapper.getNameById(recommendationcontact.getTeacherId());
           recommendationcontact.setTeacherName(name1);
       }
       return recommendationcontactArrayList;
    }

    @Override
    public ArrayList<Recommendationcontact> getSendList(Long id) {
        ArrayList<Recommendationcontact> recommendationcontactArrayList = recommendationcontactMapper.getSendList(id);
        for (Recommendationcontact s : recommendationcontactArrayList){
            s.setStudentName(userMapper.getNameById(s.getStudentId()));
            s.setTeacherName(userMapper.getNameById(s.getTeacherId()));
        }
        return recommendationcontactArrayList;
    }

    @Override
    public void acceptContact(Long contactId, String acceptInfo, String acceptMessage, String initiator) {
        if (Objects.equals(initiator, "student")){
            recommendationcontactMapper.acceptStudentContact(contactId,acceptInfo,acceptMessage);
        }else {
            recommendationcontactMapper.acceptTeacherContact(contactId, acceptInfo, acceptMessage);
        }
    }

    @Override
    public ListResponse acceptAll(Long id, int pageSize, int pageNum) {
        String type =  userMapper.getUserTypeById(id);
        ArrayList<Recommendationcontact> recommendationcontactArrayList;
        if (Objects.equals(type, "admin")){
            recommendationcontactArrayList = recommendationcontactMapper.getList(id);
        }else {
            recommendationcontactArrayList = recommendationcontactMapper.getAllAcceptContact(id);
        }
        for(Recommendationcontact recommendationcontact : recommendationcontactArrayList){
            String stuName = userMapper.getNameById(recommendationcontact.getStudentId());
            String teaName = userMapper.getNameById(recommendationcontact.getTeacherId());
            recommendationcontact.setTeacherName(teaName);
            recommendationcontact.setStudentName(stuName);
        }
        int total = recommendationcontactArrayList.size();
        int startIndex = (pageNum-1)*pageSize;
        int endIndex = startIndex + pageSize;
        if (endIndex>total) endIndex=total;
        List<Recommendationcontact> recommendationcontacts =  recommendationcontactArrayList.subList(startIndex,endIndex);
        ListResponse listResponse = new ListResponse(recommendationcontacts,total);
        return listResponse;
    }

    @Override
    public void refuseContact(Long studentId, Long teacherId) {
        recommendationcontactMapper.refuseContact(studentId,teacherId);
    }

    @Override
    public void withdrawContact(Long contactId) {
//        Recommendationcontact recommendationcontact = recommendationcontactMapper.getContact(contactId);
//        RecommendList recommendList = recommendationcontactMapper.getRecommend(recommendationcontact.getStudentId(),recommendationcontact.getTeacherId());
//        if (recommendList != null){
//            recommendationcontactMapper.updateRecommend(recommendationcontact.getStudentId(),recommendationcontact.getTeacherId());
//        }
        recommendationcontactMapper.withdrawContact(contactId);

    }

    @Override
    public ArrayList<Recommendationcontact> allContact(Long id) {
        ArrayList<Recommendationcontact> recommendationcontacts = recommendationcontactMapper.allContact(id);
        return recommendationcontacts;

    }


}
